//
//  YQLoginViewController.m
//  iyuanqi
//
//  Created by czl on 2017/7/27.
//  Copyright © 2017年 czl. All rights reserved.
//

#import "YQLoginViewController.h"

@interface YQLoginViewController ()

@property(nonatomic,weak)UITextField* textFieldUserName;
@property(nonatomic,weak)UITextField* textFieldPassword;

@end

@implementation YQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"登录";
    
    
    UIScrollView *scrollView = [UIScrollView new];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    UIView* contentView = [UIView new];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(scrollView);
        make.width.mas_equalTo(scrollView);
    }];
    
    UIImageView* imageHead = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_default_head"]];
    [contentView addSubview:imageHead];
    [imageHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.mas_equalTo(contentView);
    }];
    
    UITextField* textFieldUserName = [[UITextField alloc]initWithFrame:CGRectZero];
    textFieldUserName.placeholder = @"请输入用户名,邮箱或手机号";
    UIImageView *imageViewUserName = [[UIImageView alloc]initWithFrame:CGRectMake(-20, 0, 22, 22)];
    imageViewUserName.image = [UIImage imageNamed:@"img_account"];
    textFieldUserName.leftView = imageViewUserName;
    textFieldUserName.leftViewMode = UITextFieldViewModeAlways;
    textFieldUserName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [textFieldUserName setAutocorrectionType:UITextAutocorrectionTypeNo];
    [textFieldUserName setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [contentView addSubview:textFieldUserName];
    [textFieldUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageHead.mas_bottom).offset(50);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    self.textFieldUserName = textFieldUserName;
    
    UITextField* textFieldPassword = [[UITextField alloc]initWithFrame:CGRectZero];
    textFieldPassword.placeholder = @"请输入密码";
    UIImageView *imageViewPassword = [[UIImageView alloc]initWithFrame:CGRectMake(-20, 0, 22, 22)];
    imageViewPassword.image = [UIImage imageNamed:@"img_code"];
    textFieldPassword.leftView = imageViewPassword;
    textFieldPassword.leftViewMode = UITextFieldViewModeAlways;
    textFieldPassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textFieldPassword.secureTextEntry = YES;
    [contentView addSubview:textFieldPassword];
    [textFieldPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(textFieldUserName.mas_bottom).offset(25);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    self.textFieldPassword = textFieldPassword;
    
    UIButton* btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin.layer.cornerRadius = 20;
    btnLogin.layer.masksToBounds = YES;
    btnLogin.backgroundColor = MainColor;
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLogin addTarget:self action:@selector(onClickLogin) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnLogin];
    [btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(textFieldPassword);
        make.top.mas_equalTo(textFieldPassword.mas_bottom).offset(25);
        make.height.mas_equalTo(40);
    }];
    
    UIButton* btnRigister = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRigister.layer.cornerRadius = 20;
    btnRigister.layer.masksToBounds = YES;
    btnRigister.backgroundColor = MainColor;
    [btnRigister setTitle:@"注册" forState:UIControlStateNormal];
    [btnRigister setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRigister addTarget:self action:@selector(onClickRegister) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnRigister];
    [btnRigister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(btnLogin);
        make.top.mas_equalTo(btnLogin.mas_bottom).offset(25);
        make.height.mas_equalTo(40);
    }];
    
    UILabel* label1 = [UILabel new];
    label1.text = @"备注:忘记密码加qq群:495441012(找管理)";
    label1.textAlignment = NSTextAlignmentCenter;
    label1.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(btnRigister.mas_bottom).offset(25);
        make.centerX.mas_equalTo(contentView);
    }];
    
    UILabel* label2 = [UILabel new];
    label2.text = @"---------------  or  ---------------";
    label2.textAlignment = NSTextAlignmentCenter;
    label2.font = [UIFont systemFontOfSize:13];
    [contentView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label1.mas_bottom).offset(25);
        make.centerX.mas_equalTo(contentView);
    }];
    
    UIButton* btnQQ = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnQQ setBackgroundImage:[UIImage imageNamed:@"btn_login_qq"] forState:UIControlStateNormal];
    [contentView addSubview:btnQQ];
    [btnQQ mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label2.mas_bottom).offset(25);
        make.centerX.mas_equalTo(contentView).offset(-40);
    }];
    
    UIButton* btnWX = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnWX setBackgroundImage:[UIImage imageNamed:@"btn_login_weixin"] forState:UIControlStateNormal];
    [contentView addSubview:btnWX];
    [btnWX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label2.mas_bottom).offset(25);
        make.centerX.mas_equalTo(contentView).offset(40);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(btnWX.mas_bottom).offset(20);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)onClickLogin {
    if (_textFieldUserName.text.length < 1 || _textFieldPassword.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请填写完整登录信息!"];
        return;
    }
    [YQWebManager loginIn:_textFieldUserName.text password:_textFieldPassword.text withSuccessBlock:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } withFailureBlock:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"未知错误!"];
    }];
}

- (void)onClickRegister {
    
}

@end
